package mspdt.ubzuuch.mn;

import java.util.ArrayList;
import java.util.List;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Html;
public class ViewMapActivity extends MapActivity {

	private MapView mapVew;
	Context mContext;
	String strval;
	MapController mapController;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.map_view);
		Drawable drawable;
        HelloItemizedOverlay itemizedoverlay;
		mapVew = (MapView)findViewById(R.id.view_screen_mapview);
		mapVew.setBuiltInZoomControls(true);
		Bundle bundle = getIntent().getExtras();
		 int latvalue = bundle.getInt("lat");
		    int lonvalue = bundle.getInt("lon");
		     strval=bundle.getString("strval");
		    List<Overlay> mapOverlays = mapVew.getOverlays();
	        drawable = this.getResources().getDrawable(R.drawable.ic_launcher);
	        mapController=mapVew.getController();
	        mapVew.buildDrawingCache();
	        mapVew.setAnimationCacheEnabled(true);
	        mapVew.setClickable(true);
	        mapVew.getZoomLevel();
	        mapVew.displayZoomControls(true);
	        mapVew.getMapCenter(); 
	        itemizedoverlay = new HelloItemizedOverlay(drawable,this);
	        GeoPoint point = new GeoPoint(latvalue, lonvalue);
	        MyOverlayItem overlayitem = new MyOverlayItem(point, "Ulaanbaatar", strval, ViewMapActivity.class);
	        mapController.animateTo(point); 
	        itemizedoverlay.addOverlay(overlayitem);
	        itemizedoverlay.getCenter();
	        itemizedoverlay.getFocus();
	        mapOverlays.add(itemizedoverlay);
	        
	}
	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public void onBackPressed() {
	   finish();
	   overridePendingTransition(R.anim.no_anim, R.anim.bottom_out);
	}
	///MyOverlayItem class 
	 class MyOverlayItem extends OverlayItem {

		    private final Class<?> activityClassToOpen;

		    public MyOverlayItem (GeoPoint loc, String title, String snippet, Class<?> activityClassToOpen) {
		        super(loc, title, snippet);
		        this.activityClassToOpen = activityClassToOpen;
		    }

		    public Class<?> getActivityClassToOpen() {
		        return activityClassToOpen;
		    }
		}
	 
	 /// HelloItemizedOverlay class 
	 class HelloItemizedOverlay extends ItemizedOverlay<MyOverlayItem>
	 {
	     private final String TAG = null;
	     private ArrayList<MyOverlayItem> mOverlays = new ArrayList<MyOverlayItem>();
	     private Context mContext;
	     private Activity activity;

	     public HelloItemizedOverlay(Drawable defaultMarker, Context context)
	     {
	         super(boundCenterBottom(defaultMarker));
	         mContext = context;
	         this.activity = activity;
	     }

	     public void addOverlay(MyOverlayItem overlayitem) 
	     {
	         mOverlays.add(overlayitem);
	         populate();
	     }

	     @Override
	     protected MyOverlayItem createItem(int i)
	     {
	         return mOverlays.get(i);
	     }

	     @Override
	     public int size()
	     {
	         return mOverlays.size();
	     }
	     @Override
	     protected boolean onTap(int i)
	     {
	         OverlayItem item = mOverlays.get(i);
	         AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
	         dialog.setTitle(item.getTitle());
	         dialog.setMessage(Html.fromHtml(item.getSnippet()));
	         dialog.show();
	         return true;
	     }
	    
	 }
	 
}


